package c_leetcode.a_20_valid_parentheses;

import java.util.Stack;

/**
 * 栈
 * 时间复杂度    O(n)
 * 空间复杂度    O(n)
 *
 * @author He Kuntao
 * @since 2025/3/18 17:08
 */
public class Stacked {

    public static void main(String[] args) {
        System.out.println(new Stacked().isValid(("([])")));
    }

    public boolean isValid(String s) {
        char[] chars = s.toCharArray();
        Stack<Character> stack = new Stack<>();

        for (char c : chars) {
            if (stack.isEmpty() || !isBetween(stack.peek(), c)) {
                stack.push(c);
            } else {
                stack.pop();
            }
        }
        return stack.isEmpty();
    }

    private boolean isBetween(char peek, char aChar) {
        return aChar - peek > 0 && aChar - peek <= 2;
    }
}